//
//  ViewController.m
//  CoreData0825
//
//  Created by dllo on 15/11/20.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "ViewController.h"
#import "CoreDataManager.h"
#import "Student.h"

@interface ViewController ()
- (IBAction)add:(id)sender;
- (IBAction)cance:(id)sender;
- (IBAction)update:(id)sender;
- (IBAction)select:(id)sender;

@property(nonatomic, strong) CoreDataManager *manager;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    // 创建数据管理器
    self.manager = [CoreDataManager defaults];
    
    NSLog(@"url =========== %@", [self.manager applicationDocumentsDirectory]);

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark coredate添加方法
- (IBAction)add:(id)sender {
    
    //创建一个实体描述类
    //参数1. 描述的实体
    //参数2. 数据管理器
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Student" inManagedObjectContext:self.manager.managedObjectContext];
    
    //创建实体
    //参数1. 实体的描述
    //参数2. 数据管理器
    Student *stu = [[Student alloc] initWithEntity:entity insertIntoManagedObjectContext:self.manager.managedObjectContext];
    stu.name = @"小小";
    stu.age = @16; //integer转换成NSNumber
    stu.sex = @"?";
    stu.hobby = @"吹🐂";
    stu.number = @24;
    
    //存储数据
    [self.manager saveContext];
    
}


#pragma mark 删除
- (IBAction)cance:(id)sender {
    
    //先查询在删除
    
    
    // 获取查询数据的请求,相当于数据库的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    //添加一个约束 谓词
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"sex", @"%@"], @"?"];
    //给请求添加约束
    request.predicate = predicate;
    
    //通过某个key给数组排序
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error;
    
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    
#warning 查找的快捷键是 fet
    for (Student *stu in array) {
        
        [self.manager.managedObjectContext deleteObject:stu];
        
    }
    
    [self.manager saveContext];
   
    
    
}


//修改
- (IBAction)update:(id)sender {
    
    
    // 获取查询数据的请求,相当于数据库的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    // 添加一个约束 谓词
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"name", @"%@"], @"小小"];
    //给请求添加约束
    request.predicate = predicate;
    
    //通过某个key给数组排序
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    
    //
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error;
    
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    
    //先进行查找在修改
    Student *stu = [array lastObject];
    stu.sex = @"!";
    
    [self.manager saveContext];
    NSLog(@"%@", array);
    
    
    
}


#pragma mark 查找
- (IBAction)select:(id)sender {
    
    
    // 获取查询数据的请求,相当于数据库的查询语句
    NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Student"];
    // 添加一个约束 谓词
    NSPredicate *predicate = [NSPredicate predicateWithFormat:[NSString stringWithFormat:@"%@ == %@", @"name", @"%@"], @"小小"];
    //给请求添加约束
    request.predicate = predicate;
    
    //通过某个key给数组排序
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    
    //
    [request setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
    
    NSError *error;
    
    NSArray *array = [self.manager.managedObjectContext executeFetchRequest:request error:&error];
    
    NSLog(@"%ld", array.count);
    NSLog(@"%@", array);
    
    
    
}
@end
